package main.www.wonderfuldata.servlet;

import com.mysql.cj.Session;
import jdk.nashorn.internal.objects.annotations.Constructor;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "LoginServlet",urlPatterns = {"/login"})
public class LoginServlet extends HttpServlet {

        public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //设置字符编码
            request.setCharacterEncoding("utf8");
            //从request对象中获取username,password
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            System.out.println(username+password);

            HttpSession session = request.getSession(true);
            //判断是否为管理员
            if("administrator".equals(username)&&"123456".equals(password)){
                //登录成功，设置session

                session.setAttribute("user", "管理员，欢迎你！");
                //将页面转发到欢迎页面
                RequestDispatcher dispatcher ;

                dispatcher  = request.getRequestDispatcher("/welcome.jsp");
                dispatcher.forward(request,response);

            }else {

                session.setAttribute("user","登录信息错误，请检查用户名或密码");

            }
        }




}
